package pl.koszolko.documentclustering.cluster.document;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import pl.koszolko.documentclustering.dao.Document;
import pl.koszolko.documentclustering.dao.Factory;
import pl.koszolko.documentclustering.dao.utils.DataBaseConnectionManager;

public class FactoryDocumentForClustering {
	private static String PROPERTIES_FILE = "dbConfig.properties";

	public final static String HOST = "host";
	public final static String USER = "user";
	public final static String PASSWORD = "password";

	private static FactoryDocumentForClustering instance = null;
	private Factory factory;

	private FactoryDocumentForClustering() {
		factory = new Factory(getDatabaseConnection());
	}

	private DataBaseConnectionManager getDatabaseConnection() {
		Properties properties = new Properties();

		try {
			properties.load(new FileInputStream("config" + File.separator
					+ PROPERTIES_FILE));
		} catch (Exception e) {
			throw new RuntimeException("Blad odczytu konfiguracji bazy danych");
		}

		String host = properties.getProperty(HOST);
		String user = properties.getProperty(USER);
		String password = properties.getProperty(PASSWORD);
		DataBaseConnectionManager connectionManager = new DataBaseConnectionManager(
				host, user, password);
		return connectionManager;
	}

	public static FactoryDocumentForClustering getInstace() {
		if (instance == null)
			instance = new FactoryDocumentForClustering();
		return instance;
	}

	public List<DocumentForClustering> createDocumentForClusteringByAbstract() {
		List<Document> allDocuments = factory.getAllDocuments();
		List<DocumentForClustering> documentsForClustering = new ArrayList<DocumentForClustering>();

		for (Document doc : allDocuments) {
			DocumentClusteringByAbstractText docAbstractText = new DocumentClusteringByAbstractText(
					doc);
			documentsForClustering.add(docAbstractText);
		}

		return documentsForClustering;
	}

	public List<DocumentForClustering> createDocumentForClusteringByReferences() {
		List<Document> allDocuments = factory.getAllDocuments();
		List<DocumentForClustering> documentsForClustering = new ArrayList<DocumentForClustering>();

		for (Document doc : allDocuments) {			
			DocumentClusteringByReferences docAbstractText = new DocumentClusteringByReferences(
					doc);
			documentsForClustering.add(docAbstractText);
		}

		return documentsForClustering;
	}

	public List<DocumentForClustering> createDocumentForClusteringByCitedBy() {
		List<Document> allDocuments = factory.getAllDocuments();
		List<DocumentForClustering> documentsForClustering = new ArrayList<DocumentForClustering>();

		for (Document doc : allDocuments) {
			DocumentClusteringByCitedBy docAbstractText = new DocumentClusteringByCitedBy(
					doc);
			documentsForClustering.add(docAbstractText);
		}

		return documentsForClustering;
	}

	public List<DocumentForClustering> createDocumentForClusteringByAuthor() {
		List<Document> allDocuments = factory.getAllDocuments();
		List<DocumentForClustering> documentsForClustering = new ArrayList<DocumentForClustering>();

		for (Document doc : allDocuments) {
			DocumentClusteringByAuthor docAbstractText = new DocumentClusteringByAuthor(
					doc);
			documentsForClustering.add(docAbstractText);
		}

		return documentsForClustering;
	}

	public List<DocumentForClustering> createDocumentForClusteringBySoundexAuthor() {
		List<Document> allDocuments = factory.getAllDocuments();
		List<DocumentForClustering> documentsForClustering = new ArrayList<DocumentForClustering>();

		for (Document doc : allDocuments) {
			DocumentClusteringByAuthor docAbstractText = new DocumentClusteringBySundexAuthor(
					doc);
			documentsForClustering.add(docAbstractText);
		}

		return documentsForClustering;
	}

	public List<DocumentForClustering> createDocumentForCLusteringByReferencesAndCitedBy() {
		List<Document> allDocuments = factory.getAllDocuments();
		List<DocumentForClustering> documentsForClustering = new ArrayList<DocumentForClustering>();

		for (Document doc : allDocuments) {
			DocumentClusteringByReferencesAndCitedBy docReferencesAndCitedBy = new DocumentClusteringByReferencesAndCitedBy(
					doc);
			documentsForClustering.add(docReferencesAndCitedBy);
		}

		return documentsForClustering;
	}
}
